package com.credit.management.system.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.management.system.model.Company;
import com.credit.management.system.model.Page;

import com.credit.management.system.service.ICompanyService;


@Controller
@RequestMapping("/company")
public class CompanyController {
	@Autowired
	private ICompanyService companyService;
	
	/**
	 * 进入list页面
	 * @return
	 */
	@RequestMapping(value="/company",method=RequestMethod.GET)
	public String list(){
	
		return "company/company";	
	}
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(Integer start, Integer length, Object total)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		// 查总条数（单表查询），先去联系怎删改
		Page page=new Page(start,length);
		page=companyService.findPageCompany(page);
		
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page.getList());//返回的数据
		map.put("iTotalRecords", page.getTotal());//总条数
		map.put("iTotalDisplayRecords",page.getTotal());////总条数
		return map;
	}
	
	/**
	 * 添加
	 * @param companyname
	 * @param legalpersonname
	 * @param legalpersonidcard
	 * @param registeredcapital
	 * @param companyprofile
	 * @param companyaddress
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Integer add(Company company){
		
//	/*	System.out.println(legalpersonname);
//		System.out.println(companyname);
//		Company company=new Company();
//		company.setCompanyname(companyname);
//		company.setLegalpersonname(legalpersonname);
//		company.setLegalpersonidcard(legalpersonidcard);
//		company.setRegisteredcapital(registeredcapital);
//		company.setCompanyprofile(companyprofile);
//		company.setCompanyaddress(companyaddress);
//		company.setPhone(phone);*/
	
		int num=companyService.insertSelective(company);
		
		return num;
	}
	
	/**
	 * 根据id修改
	 * @param company
	 * @return
	 */
	@RequestMapping(value = "/updatecompany")
	@ResponseBody
	public Integer updatecompany(Company company) {
		
		Map<String,Object> map =new HashMap<>();
		
		Integer num=companyService.updateByPrimaryKeySelective(company);
		System.out.println(num);
		return num;
	}
	
	/**
	 * 根据id查找
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findbyid")
	@ResponseBody
	public Company findbyid(Integer id){
		
		Company company=companyService.selectByPrimaryKey(id);
		
		return company;
	}
	
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/deletebyid",method=RequestMethod.POST)
	@ResponseBody
	public Integer delete(Integer id){
		Integer result=0;
		
		result=companyService.deleteByPrimaryKey(id);
		return result;
	}
	
	
}
